#include "myTreeCtrl.h"

MyTreeCtrl::MyTreeCtrl(){
}

MyTreeCtrl::MyTreeCtrl(wxWindow *parent, const wxPoint& pos, const wxSize& size, long style) : wxTreeCtrl(parent, wxID_ANY, pos, size, style){
    root = AddRoot(wxT("Objetos"), -1, -1);
//    rootSolid = AppendItem(root, _("Solidos"), 0, 0);
//    rootSolid = AppendItem(root, _("Poligonos"), 0, 0);

}

MyTreeCtrl::~MyTreeCtrl(){
}

bool MyTreeCtrl::deleteItens(Scene *_s){
    wxArrayTreeItemIds selectedArray;
    size_t count = GetSelections(selectedArray);

    wxTreeItemId item;
    for ( size_t i = 0; i < count; i++ ){
        item = selectedArray.Item(i);
        if (GetItemParent(item) != root){
            return false;
        }
    }
    for ( size_t i = count; i > 0  ; i-- ){
        item = selectedArray.Item(i-1);
        _s->deleteSolid(( (MyTreeItemData*)(GetItemData(item)) )->getSolid());
        Delete(item);
    }
    return true;
}

void MyTreeCtrl::selectItens(Scene *_s){
    wxArrayTreeItemIds selectedArray;
    size_t count = GetSelections(selectedArray);
    wxTreeItemId item, id;
    wxTreeItemIdValue cookie;

    for ( size_t i = 0; i < count; i++ ){
        item = selectedArray.Item(i);
        if ( (GetItemText(item) == _("Faces")) || (GetItemText(item) == _("Arestas")) || (GetItemText(item) == _("Vertices")) ){
            SetItemBold(item, !IsBold(item));

            id = GetFirstChild(item, cookie);
            while(id.IsOk()){
                ( (MyTreeItemData*)(GetItemData(id)) )->select(IsBold(item), _s);
                SetItemBold(id, IsBold(item));
                id = GetNextSibling(id);
            }
        }
        else{
            ( (MyTreeItemData*)(GetItemData(item)) )->select(!IsBold(item), _s);
            SetItemBold(item, !IsBold(item));
            if (((MyTreeItemData*)(GetItemData(item)))->getPoint() == NULL)
                if (!IsBold(item)) SetItemBold(GetItemParent(item), false);
        }
    }
}

void MyTreeCtrl::selectAdjacentVertices(Scene *_s){
    std::vector<Vertex *> selected;
    _s->selectAdjacentVertices(&selected);

    repopulate(_s, true);
}

void MyTreeCtrl::selectAdjacentEdges(Scene *_s){
    std::vector<Edge *> selected;
    _s->selectAdjacentEdges(&selected);

    repopulate(_s, true);
}

void MyTreeCtrl::selectAdjacentFaces(Scene *_s){
    std::vector<Face *> selected;
    _s->selectAdjacentFaces(&selected);

    repopulate(_s, true);
}

void MyTreeCtrl::selectAdjacentSolids(Scene *_s){
    std::vector<Solid *> selected;
    _s->selectAdjacentSolids(&selected);

    repopulate(_s, true);

    wxTreeItemIdValue cookie;
    wxTreeItemId solid = GetFirstChild(root, cookie);

    int size = _s->sizeSolids(), size2;
    for(int i = 0; i < size; i++){
        size2 = selected.size();
        for(int j = size2; j > 0 ; j--){
            if (selected.empty()){
                break;
            }
            if( ( (MyTreeItemData*)(GetItemData(solid)) )->getSolid() == selected.at(j - 1) ){
                SetItemBold(solid);
                selected.erase(selected.begin() + j - 1);
            }
        }
        solid = GetNextSibling(solid);
    }
}

void MyTreeCtrl::repopulate(Scene *_s, bool _adjacencie){
    wxTreeItemIdValue cookie;
    wxTreeItemId solid = GetFirstChild(root, cookie), item, id;

    int size = _s->sizeSolids();
    Solid *s;
    wxString str;
    Face *f;
    Edge *e;
    Vertex *v;
    for(int i = 0; i < size; i++){
        if(_adjacencie) SetItemBold(solid, false);//solido

        item = GetFirstChild(solid, cookie);
        if(_adjacencie) SetItemBold(item, false);//face
        DeleteChildren(item);

        s = _s->getSolid(i);

        for(int j = 0; j < s->sizeFaces(); j++){
            f = s->getFace(j);
            if(f->isBezier()) str.Printf(wxT("b%d"), f->getID());
            else str.Printf(wxT("f%d"), f->getID());

            id = AppendItem(item, str, 0, 0, new MyTreeItemData(f));
            if (f->isSelected()) SetItemBold(id);

            if(f->isBezier()){
                BezierSurface *b = f->getBezier();
                int size = b->getSizeControl();
                for(int i_ = 0; i_ < size; i_++){
                    for(int j_ = 0; j_ < size; j_++){
                        str.Printf(wxT("p(%d, %d)"), i_, j_);
                        AppendItem(id, str, 0, 0, new MyTreeItemData(b->getControlPoint(i_,j_)));
                    }
                }

            }
        }


        item = GetNextSibling(item);//aresta
        if(_adjacencie) SetItemBold(item, false);//aresta
        DeleteChildren(item);

        for(int j = 0; j < s->sizeEdges(); j++){
            e = s->getEdge(j);
            str.Printf(wxT("e%d"), e->getID() );
            if (e->isSelected()) SetItemBold(AppendItem(item, str, 0, 0, new MyTreeItemData(e)));
            else AppendItem(item, str, 0, 0, new MyTreeItemData(e));
        }

        item = GetNextSibling(item);//vertice
        if(_adjacencie) SetItemBold(item, false);//vertice
        DeleteChildren(item);

        for(int j = 0; j < s->sizeVertices(); j++){
            v = s->getVertix(j);
            str.Printf(wxT("v%d (%1.2f, %1.2f, %1.2f)"), v->getID(), v->getPoint().x, v->getPoint().y, v->getPoint().z );
            if (v->isSelected()) SetItemBold(AppendItem(item, str, 0, 0, new MyTreeItemData(v)));
            else AppendItem(item, str, 0, 0, new MyTreeItemData(v));
        }

        solid = GetNextSibling(solid);
    }
}

